package util.boyaa.processManager 
{
	import com.greensock.TweenLite;
	import flash.display.MovieClip;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getTimer;
	import util.boyaa.MCUtil;
	
	/**
	 * 任务处理过程组
	 * 默认的数据类型为JSON数据，如果是其它数据来源，请重写 doAI 函数来实现
	 * @author henrylee   Any question, pls contact me. henrylee_sz@qq.com.
	 */
	public class AbstractProcessPackage implements IProcessPackage 
	{
		
		/** 任务流是否在进行中 */
		protected var isRunning: Boolean;
		/** 是倒计时还是任务全部完成控制模式 */
		protected var isDurationLimit: Boolean;
		/** 任务流完成后执行 */
		protected var onDoneFunc: Function;
		
		
		
		/** 任务数据，此处默认的数据类型为JSON数据 */
		protected var _aiData: Object;
		/** 能够解析的数据label，此处称之为能力 */
		protected var ABILITY: Vector.<Object>;
		/** 解析出来的任务队列 */
		protected var tasks: Vector.<Object>;
		
		
		/** 任务流程中临时出现的动画效果 */
		protected var exMC: Vector.<MovieClip>;
		/** 任务流程中临时出现的TweenLite */
		protected var tweens: Vector.<TweenLite>;
		/** 任务流程中临时出现的其它子任务流 */
		protected var subProcess: Vector.<IProcessPackage>;
		
		
		
		public function AbstractProcessPackage() 
		{
			exMC = new Vector.<MovieClip>();
			tweens = new Vector.<TweenLite>();
			subProcess = new Vector.<IProcessPackage>();
			tasks = new Vector.<Object>();
			
			initAbility();
		}
		
		
		/**
		 * 重写该函数
		 * 填写处理过程能识别的任务
		 */
		protected function initAbility():void
		{
			//example
			ABILITY = new Vector.<Object>();
			//ABILITY[0] = { 'key':"comattack",       'duration':2.0,       'process':doProcess };
		}
		
		
	
		
		public function doAI(aiData:Object, doneFunc:Function = null):void 
		{
			_aiData = aiData;
			isRunning = true;
			onDoneFunc = doneFunc;
			
			parseTaskList();
			doAIPackage();
		}
		
		
		/**
		 * 把活动原始数据解析成可以理解的任务队列
		 * 如果采用的是其它数据格式请重写该函数
		 */
		protected function parseTaskList():void
		{
			var len: int = ABILITY.length;
			var i:int = 0;
			while (i < len) {
				if (_aiData.hasOwnProperty(ABILITY[i]['key']))
					tasks.push( { 'process': ABILITY[i]['process'], 'data':_aiData[ABILITY[i]['key']], 'duration':ABILITY[i]['duration'] } );
				i++;
			}
		}
		
		
		/**
		 * 执行ai流程
		 */
		protected function doAIPackage():void
		{
			if (!isRunning) {
				remove();
				return;
			}
			
			if (tasks.length > 0) {
				var obj: Object = tasks.shift();
				if (isDurationLimit)
					delayCall(obj['duration'], doAIPackage);
				else
					(obj['process'] as Function).apply(null, [obj['data']]);
			}
			else
				doneProcess();
		}
		
		
		/**
		 * 任务完成后调用
		 */
		protected function doneProcess():void
		{
			remove();
			onDoneFunc != null && onDoneFunc();
		}
		
		
		/**
		 * 重写该函数实现具体的操作
		 * 可以按任务功能取名称，例如：processNormalAttack
		 * 
		 * @param   params                    任务数据
		 * 
		 */
		protected function doProcess(params: Object):void
		{
			
		}
		
		
		public function remove():void
		{
			trace(getQualifiedClassName(this), " remove! ", getTimer());
			isRunning = false;
			
			var ___sub: IProcessPackage;
			while (subProcess.length) {
				___sub = subProcess.pop();
				___sub.running && ___sub.remove();
			}
			
			var __tw:TweenLite;
			while (tweens.length) {
				__tw = tweens.pop();
				TweenLite.killTweensOf(__tw.target);
			}
			
			var mc: MovieClip;
			while (exMC.length) {
				mc = exMC.pop();
				MCUtil.stopAni(mc);
				mc.parent && mc.parent.removeChild(mc);
			}
		}
		
		protected function delayCall(delay:Number, onComplete:Function, onCompleteParams:Array = null):void
		{
			tweens.push(TweenLite.delayedCall(delay, onComplete, onCompleteParams));
		}
		
		public function get running():Boolean { return isRunning; }
	}

}